package com.appos.web.controller;

import com.alibaba.fastjson.JSONObject;
import com.appos.common.core.controller.BaseController;
import com.appos.common.core.domain.AjaxResult;
import com.appos.common.utils.StringUtils;
import com.appos.domain.AppWorkSheetRelation;
import com.appos.domain.WorkSheet;
import com.appos.domain.WorkSheetGroupRelation;
import com.appos.service.AppWorkSheetRelationService;
import com.appos.service.WorkSheetGroupRelationService;
import com.appos.service.WorkSheetService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
public class WorkSheetController extends BaseController {
    @Autowired
    private WorkSheetService workSheetService;

    @Autowired
    private AppWorkSheetRelationService appWorkSheetRelationService;

    @Autowired
    private WorkSheetGroupRelationService workSheetGroupRelationService;

    @PostMapping("/workSheet/save")
    public AjaxResult save(@RequestBody JSONObject postData){
        AppWorkSheetRelation appWorkSheetRelation = new AppWorkSheetRelation();
        String workSheetId = (String) postData.get("workSheetId");
        String appId = (String) postData.get("appId");
        appWorkSheetRelation.setWorkSheetId(workSheetId);
        appWorkSheetRelation.setAppId(appId);

        WorkSheet workSheet = new WorkSheet();
        workSheet.setWorkSheetId(workSheetId);
        String workSheetName = (String) postData.get("workSheetName");
        workSheet.setWorkSheetName(workSheetName);
        String collectionsName = (String) postData.get("collectionsName");
        workSheet.setCollectionsName(collectionsName);
        JSONObject widgetForm = (JSONObject) JSONObject.parseObject(JSONObject.toJSONString(postData.get("widgetForm")));
        workSheet.setWidgetForm(widgetForm);
        appWorkSheetRelationService.save(appWorkSheetRelation);

        String workSheetGroupUuid = (String) postData.get("workSheetGroupUuid");
        if(StringUtils.isNotNull(workSheetGroupUuid)) {
            WorkSheetGroupRelation workSheetGroupRelation = new WorkSheetGroupRelation();
            workSheetGroupRelation.setWorkSheetGroupUuid(workSheetGroupUuid);
            workSheetGroupRelation.setWorkSheetId(workSheetId);
            workSheetGroupRelationService.save(workSheetGroupRelation);
        }
        return toAjax(workSheetService.save(workSheet));
    }

    @GetMapping("/workSheet/getWorkSheet")
    public AjaxResult getWorkSheet(@RequestParam String workSheetId){
        return AjaxResult.success(workSheetService.getWorkSheet(workSheetId));
    }

    @GetMapping("/workSheet/getWorkSheetList")
    public AjaxResult getWorkSheetList(@RequestParam String appId, @RequestParam(value="workSheetGroupUuid", required=false) String workSheetGroupUuid){
        List list = null;
        if(StringUtils.isNotNull(workSheetGroupUuid)){
            List<WorkSheetGroupRelation> l = workSheetGroupRelationService.getWorkSheetGroupRelationList(workSheetGroupUuid);

            List<String> ids = new ArrayList<>();
            l.forEach(o -> {
                ids.add(o.getWorkSheetId());
            });
            list = workSheetService.getWorkSheetList(ids);
        } else {
            List<AppWorkSheetRelation> l = appWorkSheetRelationService.getAppWorkSheetRelation(appId);

            List<String> ids = new ArrayList<>();
            l.forEach(o -> {
                ids.add(o.getWorkSheetId());
            });
            list = workSheetService.getWorkSheetList(ids);
        }

        return AjaxResult.success(list);
    }

    @PostMapping("/workSheet/update")
    public AjaxResult edit(@RequestBody WorkSheet workSheet){
        return toAjax(workSheetService.updateWorkSheet(workSheet));
    }
}
